package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.Credit;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 统计
 */
@Component
public class CreditDao implements BaseDao<Credit> {
    @Override
    public int insert(Credit entity) {
        return 0;
    }

    @Override
    public int update(Credit entity) {
        return 0;
    }

    @Override
    public int delete(Credit entity) {
        return 0;
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return null;
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        Credit c = (Credit)obj[0];
        String sql1 = " select ordernumber,totalprice,gmt_create from room_predict_order where is_status=0  ";
        String sql2 = "  UNION ALL  select order_number,total_price,gmt_create from sale_order where is_status=0  ";
        String sql3 = "  UNION ALL  select ordernumber,totalprice,gmt_create from  room_real_order where is_status=0 ";
        if (c.getOrdernumber() != null && !"".equals(c.getOrdernumber())) {
            sql1 += " and ordernumber like '%" + c.getOrdernumber() + "%' ";
            sql2 += " and order_number like '%" + c.getOrdernumber() + "%' ";
            sql3 += " and ordernumber like '%" + c.getOrdernumber() + "%' ";
        }
        String sql = sql1 + sql2 + sql3;
        return DBHelper.executeQuery(sql + " limit ?,? ", page.getBegin(), page.getPageSize());
    }

    @Override
    public Map<String, Object> findOne(Credit entity) {
        return null;
    }

    @Override
    public int count(Object... obj) {
        Credit c = (Credit)obj[0];
        String sql1 =
            " select sum(a.b) as num from ( " + "  select count(*) as b from room_predict_order where is_status=0 ";
        String sql2 = "union all  select count(*) as b from sale_order where is_status=0 ";
        String sql3 = "union all select count(*) as b from room_real_order where is_status=0";
        String sql4 = " ) as a";

        if (c.getOrdernumber() != null && !"".equals(c.getOrdernumber())) {
            sql1 += " and ordernumber like '%" + c.getOrdernumber() + "%' ";
            sql2 += " and order_number like '%" + c.getOrdernumber() + "%' ";
            sql3 += " and ordernumber like '%" + c.getOrdernumber() + "%' ";
        }
        String sql = sql1 + sql2 + sql3 + sql4;
        // System.out.println(sql);
        return Integer.valueOf(DBHelper.executeQuery(sql).get(0).get("num").toString());
    }
}
